package cn.jbolt.core.render;

import cn.jbolt.core.base.JBoltRequestType;
import cn.jbolt.core.consts.JBoltConst;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.render.ErrorRender;
import com.jfinal.render.Render;
import com.jfinal.render.RenderException;
import com.jfinal.render.RenderManager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
 * JBoltErrorRender定制
 * @ClassName:  JBoltErrorRender
 * @author: JFinal学院-小木 QQ：909854136
 * @date:   2022年3月16日
 */
public class JBoltErrorRender extends Render {
	private static Ret RET_404 = Ret.fail("404,您访问的资源不存在");
	private static Ret RET_500 = Ret.fail("500,服务异常");
	private static Ret RET_401 = Ret.fail("401,未经许可授权的请求");
	private static Ret RET_403 = Ret.fail("403,拒绝访问");
	public static void set404ErrorMsg(String msg) {
		RET_404.set("msg", msg);
	}
	public static void set500ErrorMsg(String msg) {
		RET_500.set("msg", msg);
	}
	public static void set401ErrorMsg(String msg) {
		RET_401.set("msg", msg);
	}
	public static void set403ErrorMsg(String msg) {
		RET_403.set("msg", msg);
	}
	private int errorCode;
	public JBoltErrorRender(int errorCode) {
		this.errorCode = errorCode;
	}
	@Override
	public void render() {
		response.setStatus(getErrorCode());	// HttpServletResponse.SC_XXX_XXX
		Object rqTypeObj = request.getAttribute(JBoltConst.RQKEY_JB_RQTYPE);
		if(rqTypeObj == null) {
			renderDefaultError(request, response);
			return;
		}
		String rqType = rqTypeObj.toString().trim();
		if (StrKit.isBlank(rqType)) {
			renderDefaultError(request,response);
			return;
		}
		switch (rqType) {
		case JBoltRequestType.JBOLTAPI:
			renderJson(request, response);
			break;
		case JBoltRequestType.AJAX:
			renderJson(request, response);
			break;
		case JBoltRequestType.JSON:
			renderJson(request, response);
			break;
		case JBoltRequestType.PJAX:
			renderPjax(request, response);
			break;
		case JBoltRequestType.AJAXPORTAL:
			renderAjaxPortal(request,response);
			break;
		case JBoltRequestType.DIALOG:
			renderDefaultError(request,response);
			break;
		case JBoltRequestType.IFRAME:
			renderDefaultError(request,response);
			break;
		case JBoltRequestType.NORMAL:
			renderDefaultError(request,response);
			break;
		case JBoltRequestType.NULL:
			renderDefaultError(request,response);
			break;
		default:
			renderDefaultError(request,response);
			break;
		}
	}
	private void setMsg(HttpServletRequest request) {
		String msg = null;
		switch (errorCode) {
			case HttpServletResponse.SC_NOT_FOUND:
				msg=RET_404.getStr("msg");
				break;
			case HttpServletResponse.SC_INTERNAL_SERVER_ERROR:
				msg=RET_500.getStr("msg");
				break;
			case HttpServletResponse.SC_UNAUTHORIZED:
				msg=RET_401.getStr("msg");
				break;
			case HttpServletResponse.SC_FORBIDDEN:
				msg=RET_403.getStr("msg");
				break;
			default:
				msg=RET_500.getStr("msg");
				break;
		}
		request.setAttribute("msg", msg);
	}
	private void renderAjaxPortal(HttpServletRequest request, HttpServletResponse response) {
		setMsg(request);
		RenderManager.me().getRenderFactory().getRender("/_view/_admin/common/msg/portalerror.html").setContext(request, response).render();
	}
	private void renderPjax(HttpServletRequest request, HttpServletResponse response) {
		setMsg(request);
		RenderManager.me().getRenderFactory().getRender("/_view/_admin/common/msg/pjaxerror.html").setContext(request, response).render();
	}
	private void renderDefaultError(HttpServletRequest request, HttpServletResponse response) {
		new ErrorRender(errorCode,ErrorRender.getErrorView(errorCode)).setContext(request, response).render();
	}

	private void renderJson(HttpServletRequest request, HttpServletResponse response) {
		PrintWriter writer = null;
		try {
			response.setContentType("application/json; charset=utf-8");
			writer = response.getWriter();
			String json = null;
			switch (errorCode) {
				case HttpServletResponse.SC_NOT_FOUND:
					json = JsonKit.toJson(RET_404);
					break;
				case HttpServletResponse.SC_INTERNAL_SERVER_ERROR:
					json = JsonKit.toJson(RET_500);
					break;
				case HttpServletResponse.SC_UNAUTHORIZED:
					json = JsonKit.toJson(RET_401);
					break;
				case HttpServletResponse.SC_FORBIDDEN:
					json = JsonKit.toJson(RET_403);
					break;
				default:
					json = JsonKit.toJson(RET_500);
					break;
			}
			writer.write(json);
			writer.flush();
		} catch (IOException e) {
			throw new RenderException(e);
		}
	}

	public int getErrorCode() {
		return errorCode;
	}

}
